package monaco;

import data.ZProjuectData.ZProjectData;

class Api {
	public static function main():Void {
		untyped window.XmlEditorContent = XmlEditorContent;
	}
}

@:expose
class XmlEditorContent {
	/**
	 * 项目路径
	 */
	public static var projectPath:String;

	public static var exists:String->Bool;

	public static var readFile:(path:String) -> String;

	public static var readDir:(path:String) -> Array<String>;

	public static var currentProjectData:ZProjectData;

	public static function registerZProjectData(data:ZProjectData):Void {
		currentProjectData = data;
	};

	/**
	 * 初始化提示缓存
	 */
	public function new():Void {}

	/**
	 * 智能提示逻辑
	 * @param model
	 * @param position
	 * @return Dynamic
	 */
	public function provideCompletionItems(model:Dynamic, position:Dynamic, context:{triggerKind:Int, triggerCharacter:String}, token:Dynamic):SuggestionsData {
		trace("model=", model);
		trace("position=", position);
		trace("context=", context);
		trace("token=", token);
		var line:Int = position.lineNumber;
		var column = position.column;
		var content:String = model.getLineContent(line);
		var leftInput = content.substr(0, column - 1);
		if (context.triggerCharacter == ":") {
			var index = content.indexOf("<") + 1;
			var leftToken = content.substr(index, (column - 2) - index);
			if (leftToken == "xml") {
				return CommonentProvideCompletionItems.getXmlFileConfig(currentProjectData, leftToken + ":");
			} else {
				var index = leftInput.lastIndexOf("\"", column - 1);
				var endIndex = leftInput.indexOf(":", index);
				if (index != -1 && endIndex != -1) {
					var spriteId = leftInput.substr(index + 1, endIndex - index - 1);
					trace("leftToken=", leftToken, leftInput, spriteId);
					return CommonentProvideCompletionItems.getSpriteIdsProperties(leftInput, spriteId);
				}
			}
		} else if (context.triggerCharacter == "<") {
			// 展示可选组件
			return CommonentProvideCompletionItems.getClasses(leftInput);
		} else if (context.triggerCharacter == "\"") {
			var char = content.charAt(column - 3);
			var index = content.lastIndexOf(" ", column - 1);
			var endIndex = content.indexOf("=", index);
			if (index != -1 && endIndex != -1 && char == "=") {
				var attrId = content.substr(index + 1, endIndex - index - 1);
				if (attrId == "src" || attrId == "path") {
					return CommonentProvideCompletionItems.getResources(leftInput, attrId == "src");
				}
			}
		} else if (context.triggerCharacter == "/") {
			var leftToken = content.charAt(column - 3);
			if (leftToken == "<") {
				return CommonentProvideCompletionItems.getClasses(leftInput);
			} else {
				// 资源路径检测
				return CommonentProvideCompletionItems.getResources(leftInput);
			}
		} else if (context.triggerCharacter == " ") {
			// 查找属性
			var index = content.lastIndexOf("<", column - 1);
			var endIndex = content.indexOf(" ", index);
			if (index != -1 && endIndex != -1) {
				var classId = content.substr(index + 1, endIndex - index - 1);
				return CommonentProvideCompletionItems.getProperties(leftInput, content, classId);
			}
		}
		return {
			suggestions: []
		};
	}

	public var triggerCharacters:Array<String> = ["<", " ", "/", "\"", ":"];
}
